<?php
class LodeController extends Controller
{
    public function actionIndex()
    {
        $this->actionList();
    }

    public function actionList(){
        $url = new Url();
        $str_boso = Array();
        $ticket_detail = Array();       
        $thu = DateUtils::convertThuToNumber();
        $province = AdminProvince::getRowsByDay($thu);

            $province_id = isset($_GET['pid'])?$_GET['pid']:"";
            $kieu_danh = isset($_GET['type'])?$_GET['type']:"";
            $ticket = AdminTicket::countRowByProvinceAndType($province_id,$kieu_danh);
            //phan trang
            $page = isset($_GET['page']) ? intval($_GET ['page']):1;            
            $count = $ticket['count']; 
            $rows_per_page = 15;            
            if($count % $rows_per_page == 0){
                $totalpage = floor($count/$rows_per_page);
            } else{
                $totalpage = floor($count/$rows_per_page) + 1;                             
            }
            $get_url = "?pid=".$province_id."&type=".$kieu_danh;
            $begin = ($page - 1)*$rows_per_page;
            $end = $rows_per_page;
            $data = AdminTicket::getRowByProvinceUserAndType($province_id,$kieu_danh,$begin,$end);
            $page = Paging::showPageNavigation($page,$totalpage,$url->createUrl('lode/list').$get_url."&");
            //het phan trang

            $count_data = count($data);
            for($i = 0; $i < $count_data; $i++){
                $str_boso[$i]='';
                $ticket_detail[$i] = AdminTicketDetail::getRowByTicketId($data[$i]['id']);
                for($j=0;$j<count($ticket_detail[$i]);$j++){
                    $str_boso[$i].=$ticket_detail[$i][$j]['boso']."(".$ticket_detail[$i][$j]['xu']."xu), ";      
                }
            }               
            $this->render('list',array('province'=>$province,'count_data'=>$count_data,'ticket'=>$data,'ticket_detail'=>$ticket_detail,'str_boso'=>$str_boso,'page'=>$page,'begin'=>$begin));    
    }

    public function actionWinner(){
        $url = new Url();
        $type = Array();
        $str_boso = Array();
        $ticket_detail = Array();              
        $thu = DateUtils::convertThuToNumber();
        $province = AdminProvince::getRowsByDay($thu);

        $province_id = isset($_GET['pid'])?$_GET['pid']:"";
        $kieu_danh = isset($_GET['type'])?$_GET['type']:"";
        
        $ticket = AdminTicket::countRowByTienthuong($province_id,$kieu_danh);
        $count = $ticket['count'];
        //phan trang        
        $page = isset($_GET['page']) ? intval($_GET ['page']):1;            
        
        $rows_per_page = 15;            
        if($count % $rows_per_page == 0){
            $totalpage = floor($count/$rows_per_page);
        } else{
            $totalpage = floor($count/$rows_per_page) + 1;                             
        }
        
        $get_url = "?pid=".$province_id."&type=".$kieu_danh;
        $begin = ($page - 1)*$rows_per_page;
        $data = AdminTicket::getRowByTienthuong($province_id,$kieu_danh,$begin,$rows_per_page);
        
        $path = $url->createUrl('lode/winner').$get_url;
        $page = Paging::showPageNavigation($page,$totalpage,$path.'&');
        
        $count_data = count($data);
        //het phan trang

        for($i = 0; $i < $count_data; $i++){
            $str_boso[$i]='';
            $ticket_detail[$i] = AdminTicketDetail::getRowByTicketIdAndTienthuong($data[$i]['id']);
            $str_boso[$i] = "";
            for($j=0;$j<count($ticket_detail[$i]);$j++){
                    $str_boso[$i] .= $ticket_detail[$i][$j]['boso']."(".$ticket_detail[$i][$j]['xu']."xu), ";      
            }         
        }
        $this->render('winner',array('province'=>$province,'ticket'=>$data,'count_data'=>$count_data,'ticket_detail'=>$ticket_detail,'str_boso'=>$str_boso,'page'=>$page,'begin'=>$begin));             
    }

    public function actionCongso(){
        $lo                     = 1;
        $de                     = 2;
        $xien                   = 3;
        $total_datcuoc_lo       = 0;
        $total_tienthuong_lo    = 0;
        $total_datcuoc_de       = 0;
        $total_tienthuong_de    = 0;
        $total_datcuoc_xien     = 0;
        $total_tienthuong_xien  = 0;
        $ticket_detail_lo       = Array();
        $ticket_detail_de       = Array();
        $ticket_detail_xien     = Array();
        
        $from   = isset($_POST['from']) ?$_POST['from'] :date('d-m-Y',time()); 
        $to     = isset($_POST['to'])   ?$_POST['to']   :date('d-m-Y',time());      
        $date_from  = DateUtils::convertViDatetoDatedb($from);              
        $date_to    = DateUtils::convertViDatetoDatedb($to);
        
        $province   = AdminProvince::getAllRows();
        $province_id = isset($_POST['province_id'])?$_POST['province_id']:0;
        
        $ticket_lo      = AdminTicket::getRowByDateTypeAnhProvince($lo,$province_id,$date_from,$date_to);
        $ticket_de      = AdminTicket::getRowByDateTypeAnhProvince($de,$province_id,$date_from,$date_to);
        $ticket_xien    = AdminTicket::getRowByDateTypeAnhProvince($xien,$province_id,$date_from,$date_to);

        for($i=0;$i<count($ticket_lo);$i++){
            $ticket_detail_lo[$i] = AdminTicketDetail::getRowByTicketId($ticket_lo[$i]['id']);
            if(isset($ticket_detail_lo[$i])){
                for($j=0;$j<count($ticket_detail_lo[$i]);$j++){
                    if(isset($ticket_detail_lo[$i][$j]['xu'])){
                        $total_datcuoc_lo = $total_datcuoc_lo + $ticket_detail_lo[$i][$j]['xu'];
                        $total_tienthuong_lo= $total_tienthuong_lo + $ticket_detail_lo[$i][$j]['tien_thuong'];
                    }
                }
            }
        }
        for($i=0;$i<count($ticket_de);$i++){
            $ticket_detail_de[$i] = AdminTicketDetail::getRowByTicketId($ticket_de[$i]['id']);
            if(isset($ticket_detail_de[$i])){ 
                for($j=0;$j<count($ticket_detail_de[$i]);$j++){
                    if(isset($ticket_detail_de[$i][$j]['xu'])){
                        $total_datcuoc_de = $total_datcuoc_de + $ticket_detail_de[$i][$j]['xu'];
                        $total_tienthuong_de= $total_tienthuong_de + $ticket_detail_de[$i][$j]['tien_thuong'];    
                    }                   
                }
            }
        }
        for($i=0;$i<count($ticket_xien);$i++){
            $ticket_detail_xien[$i] = AdminTicketDetail::getRowByTicketId($ticket_xien[$i]['id']);
            if(isset($ticket_detail_xien[$i])){ 
                for($j=0;$j<count($ticket_detail_xien[$i]);$j++){
                    if(isset($ticket_detail_xien[$i][$j]['xu'])){
                        $total_datcuoc_xien = $total_datcuoc_xien + $ticket_detail_xien[$i][$j]['xu'];
                        $total_tienthuong_xien= $total_tienthuong_xien + $ticket_detail_xien[$i][$j]['tien_thuong'];    
                    }                   
                }
            }
        }
        $this->render('congso',array('province'=>$province,
                                    'province_id'=>$province_id,
                                    'from'=>$from,
                                    'to'=>$to, 
                                    'ticket_detail_lo'=>$ticket_detail_lo,
                                    'ticket_detail_de'=>$ticket_detail_de,
                                    'ticket_detail_xien'=>$ticket_detail_xien,
                                    'ticket_lo'=>$ticket_lo,
                                    'ticket_de'=>$ticket_de,
                                    'ticket_xien'=>$ticket_xien,
                                    'total_tienthuong_lo'=>$total_tienthuong_lo,
                                    'total_datcuoc_lo'=>$total_datcuoc_lo,
                                    'total_tienthuong_de'=>$total_tienthuong_de,
                                    'total_datcuoc_de'=>$total_datcuoc_de,
                                    'total_tienthuong_xien'=>$total_tienthuong_xien,
                                    'total_datcuoc_xien'=>$total_datcuoc_xien));
    }

    public function actionThongke(){
       $url = new Url();
        $type = Array();
        $str_boso = Array();
        $ticket_detail = Array();       
        $province = AdminProvince::getAllRows();
        
            $from           = isset($_GET['begin'])         ?$_GET['begin']:date("d-m-Y",time()-2592000);  
            $to             = isset($_GET['end'])           ?$_GET['end']:date("d-m-Y",time()); 
            $province_id    = isset($_GET['pid'])           ?$_GET['pid']:0;
            $kieu_danh      = isset($_GET['type'])          ?$_GET['type']:0;
            
            $begin          = DateUtils::convertViDatetoDatedb($from);
            $end            = DateUtils::convertViDatetoDatedb($to);
                                                              
            $row = AdminTicket::countAllRow($province_id,$kieu_danh,$begin,$end);
            $count_row = $row['count']; 
            
            //phan trang
            $page = isset($_GET['page']) ? intval($_GET ['page']):1;            
            
            $rows_per_page = 15;            
            if($count_row % $rows_per_page == 0){
                $totalpage = floor($count_row/$rows_per_page);
            } else{
                $totalpage = floor($count_row/$rows_per_page) + 1;                             
            }

            $numPage = ($page - 1)*$rows_per_page;
            
            $ticket = AdminTicket::getAllRowInDate($province_id,$kieu_danh,$begin,$end,$numPage,$rows_per_page);
            $count_ticket = count($ticket);
            $get_url = "?pid=".$province_id."&type=".$kieu_danh."&begin=".$from."&end=".$to; 
            $path = $url->createUrl('lode/thongke');
            $page = Paging::showPageNavigationMore($page,$totalpage,$path.$get_url."&");

            //het phan trang
            
            for($i=0;$i<count($ticket);$i++){
                $str_boso[$i]='';
                $ticket_detail[$i] = AdminTicketDetail::getRowByTicketId($ticket[$i]['id']);
                $str_boso[$i] = "";
                for($j=0;$j<count($ticket_detail[$i]);$j++){
                    $str_boso[$i] .= $ticket_detail[$i][$j]['boso']."(".$ticket_detail[$i][$j]['xu']."xu)".", ";       
                }
            }
            
            $this->render('thongke',array('province'=>$province,
                                            'ticket'=>$ticket,
                                            'ticket_detail'=>$ticket_detail,
                                            'str_boso'=>$str_boso,
                                            'province_id'=>$province_id,
                                            'kieu_danh'=>$kieu_danh,
                                            'begin'=>$begin,
                                            'end'=>$end,
                                            'page'=>$page,
                                            'numPage'=>$numPage));       
        }
        
    public function actionGiaithuong(){
            $url = new Url();
            $data = array();
            if(isset($_GET['id'])){
                $data = AdminAwards::getRowById($_GET['id']);
            }
            $row = AdminAwards::countAllRow();
            $count_row = $row['count'];
            
            $page = isset($page)?intval($page):1;
            $rows_per_page = 10;   
            $begin = ($page - 1)*$rows_per_page;                        
            $end = $rows_per_page;

            if($count_sale % $rows_per_page == 0){
                $total_page = floor($count_row/$rows_per_page);
            } else{
                $total_page = floor($count_row/$rows_per_page) + 1;                             
            }
            
            $awards = AdminAwards::getAllRowPaging($begin,$end);
            $page = Paging::showPageNavigationMore($page,$total_page,$url->createUrl("lode/giaithuong")."?");
            $count_awards = count($awards);
            $this->render("giaithuong",array("awards"=>$awards,"begin"=>$begin,"count_awards"=>$count_awards,"page"=>$page,"data"=>$data));
        }
        
    public function actionInsertrow(){
            $name           = isset($_POST['name'])?$_POST['name']:"";
            $value          = isset($_POST['value'])?$_POST['value']:"";  
            $type           = isset($_POST['type'])?intval($_POST['type']):""; 
            $description    = isset($_POST['description'])?$_POST['description']:"";
            $status         = isset($_POST['status'])?intval($_POST['status']):"";
            $image          = isset($_POST['filepath'])?$_POST['filepath']:""; 
            $order          = isset($_POST['order'])?$_POST['order']:"";

            $insert = AdminAwards::insertRow($name,$value,$description,$type,$image,$status,$order); 
            echo $insert; 
        }
        
    public function actionDeleterow(){
        $id = isset($_POST['id'])?$_POST['id']:"";        
        $delete = AdminAwards::deleteRowById($id);
        echo $delete;
    }
        
    public function actionUpdateRow(){
        $name           = isset($_POST['name'])?$_POST['name']:"";
        $value          = isset($_POST['value'])?$_POST['value']:"";  
        $type           = isset($_POST['type'])?intval($_POST['type']):""; 
        $description    = isset($_POST['description'])?$_POST['description']:"";
        $status         = isset($_POST['status'])?intval($_POST['status']):"";
        $image          = isset($_POST['filepath'])?$_POST['filepath']:""; 
        $id             = isset($_POST['id'])?$_POST['id']:"";
        $order          = isset($_POST['order'])?$_POST['order']:""; 
        $update = AdminAwards::updateRowById($id,$name,$value,$description,$type,$image,$status,$order); 
        echo $update;            
    }       
    
}
